﻿using System;
using System.Net;

namespace SohuWeiBoBatchTool
{
    /// <summary>
    /// 支持 Session 和 Cookie 的 WebClient。
    /// </summary>
    class WebClientWithCookie:WebClient
    {
        // Cookie 容器
        private CookieContainer cookieContainer;
        
        /// <summary>
        /// 创建一个新的 WebClient 实例。
        /// </summary>
        public WebClientWithCookie()
        {
            this.cookieContainer = new CookieContainer();
        }

        /// <summary>
        /// 创建一个新的 WebClient 实例。
        /// </summary>
        /// <param name="cookie">Cookie 容器</param>
        public WebClientWithCookie(CookieContainer cookies)
        {
            this.cookieContainer = cookies;
        }

        public WebClientWithCookie(string cookiestring)
        {
            // TODO: Complete member initialization
            //this.cookiestring = cookiestring;
            //String 的Cookie　要转成　Cookie型的　并放入 CookieContainer中
            this.cookieContainer = new CookieContainer();
            string[] cookstr = cookiestring.Split(';');
            foreach (string str in cookstr)
            {
                int equalIndex = str.IndexOf('=');
                //string[] cookieNameValue = str.Split('=');
                string leftvalue = str.Substring(0, equalIndex);
                string rightvalue = str.Substring(equalIndex + 1, str.Length - leftvalue.Length - 1);
                Cookie ck = new Cookie(leftvalue.Trim(), rightvalue.Trim());
                ck.Domain = "t.sohu.com";//必须写对
                this.cookieContainer.Add(ck);
            }
            string pprdg = "GfRNJLtqh0laLOYwO1JtSDECRqeaYDlxZ6YHTwqaWRSEnu3t4SDmNaKlUGTbAT0t1yeZ2Twzrr-nu-P82DyNkrlBoXmZQLYnxDBgiyqgitO8WTpnzO68_2jjjqAs6vcDa4A3M04nJt7F4FzA9RvvBEr5yFn9FQRMA_LhFgCAjQw=";
            Cookie ckpprdg = new Cookie("pprdg", pprdg);
            ckpprdg.Domain = "t.sohu.com";
            this.cookieContainer.Add(ckpprdg);
//            GET http://t.sohu.com/u/98459 HTTP/1.1
//Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
//Accept-Language: zh-CN
//Accept-Encoding: gzip, deflate
//User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; MS-RTC LM 8; .NET4.0C; InfoPath.3; .NET4.0E)
//Connection: Keep-Alive
//Host: t.sohu.com
//Cookie: SRCID=10000|00000; TWUID=25692105; JSESSIONID=abcRodrA5zgTv89XkO42s; SUV=1101242324326959; IPLOC=CN1101; pprdig=GfRNJLtqh0laLOYwO1JtSDECRqeaYDlxZ6YHTwqaWRSEnu3t4SDmNaKlUGTbAT0t1yeZ2Twzrr-nu-P82DyNkrlBoXmZQLYnxDBgiyqgitO8WTpnzO68_2jjjqAs6vcDa4A3M04nJt7F4FzA9RvvBEr5yFn9FQRMA_LhFgCAjQw=; ppinf=2|1295882685|1611242685|dXNlcmlkOjE5OnN0b25lcGV0ZXJAZm9jdXMuY258dWlkOjc6ZjU2NTc5OXxzZXJ2aWNldXNlOjIwOjAwMDAxMDAwMDAwMTAwMDAwMDAwfGNydDowOnxlbXQ6MTowfGFwcGlkOjQ6MTAwNXx0cnVzdDoxOjF8cGFydG5lcmlkOjE6MHxyZWxhdGlvbjowOnw=; lastdomain=1297092285|c3RvbmVwZXRlckBmb2N1cy5jbnw=|focus.cn


            //this.Headers
        }

        /// <summary>
        /// Cookie 容器
        /// </summary>
        public CookieContainer Cookies
        {
            get { return this.cookieContainer; }
            set { this.cookieContainer = value; }
        }

        /// <summary>
        /// 返回带有 Cookie 的 HttpWebRequest。
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.CookieContainer = cookieContainer;
                httpRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                    //Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
//Accept-Language: zh-CN
                //httpRequest.TransferEncoding = "gzip, deflate";//Accept-Encoding: gzip, deflate
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; MS-RTC LM 8; .NET4.0C; InfoPath.3; .NET4.0E)";//User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; MS-RTC LM 8; .NET4.0C; InfoPath.3; .NET4.0E)
                httpRequest.KeepAlive = true;//Connection: Keep-Alive
//Host: t.sohu.com
            }
            return request;
        }
    }
}
